import {
  request
} from '../../utils/request'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 'COURSE',
    from: '',
    userId: '',
    userInfo: {},
    dataList: [],
    showReward: false,
    page: 1,
    size: 10,
    showNoMore: false,
    showLoading: false,
    showMessage: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.from) {
      this.setData({
        from: options.from
      })
    }
    this.setData({
      userId: options.userId
    })
    this.getUserInfo()
    this.getDataList()
  },

  getIsShowTeam() {
    request({
      url: '/api/cfg/config/queryByType/MINI'
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          showMessage: resdata.data[0].value
        })
      } else {
        wx.showToast({
          title: resdata.message,
        })
      }
    })
  },

  getUserInfo() {
    request({
        url: '/mini/api/mine/queryUserInfo',
        data: {
          userId: this.data.userId
        },
        method: 'POST',
        options: {
          needLogin: false
        }
      })
      .then((resdata) => {
        if (resdata.code == 200) {
          this.setData({
            userInfo: resdata.data
          })
        } else {
          this.setData({
            userInfo: {}
          })
        }
      })
      .catch((err) => {
        console.log(err)
      })
  },

  getDataList() {
    request({
        url: '/mini/api/mine/queryUserPageData',
        data: {
          page: this.data.page,
          size: this.data.size,
          type: this.data.currentTab,
          userId: this.data.userId
        },
        method: 'POST',
        options: {
          needLogin: false
        }
      })
      .then((resdata) => {
        if (resdata.code == 200) {
          let newData = this.data.showLoading ?
            this.data.dataList.concat(
              resdata.data.data
            ) :
            resdata.data.data
          let showNoMore =
            resdata.data.data.length < 10
          this.setData({
            dataList: newData || [],
            showLoading: false,
            showNoMore: showNoMore
          })
        } else {
          wx.showToast({
            title: resdata.message,
            icon: 'none'
          })
          this.setData({
            dataList: [],
            showLoading: false,
            showNoMore: false
          })
        }
      })
      .catch((err) => {
        console.log(err)
      })
  },

  toMedal() {
    wx.navigateTo({
      url: '/packageMy/medal/medal?userId=' +
        this.data.userId
    })
  },

  onChange({
    detail
  }) {
    this.setData({
      currentTab: detail.name,
      page: 1
    })
    this.setData({
      dataList: []
    })
    this.getDataList()
  },

  toCoursesDetail(item) {
    wx.navigateTo({
      url: '/packagePlan/coursesDetail/coursesDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  toWorkDetail(item) {
    wx.navigateTo({
      url: '/packagePlan/workDetail/workDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  toActivity(item) {
    wx.navigateTo({
      url: '/packagePlan/activityDetail/activityDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  toRankingDetail(item) {
    wx.navigateTo({
      url: '/packageSquare/rankingDetail/rankingDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  toWork(item) {
    wx.navigateTo({
      url: '/packagePlan/workDetail/workDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  toFollowList() {
    wx.navigateTo({
      url: '/packageMy/followList/followList?id=' +
        this.data.userId
    })
  },

  toFansList() {
    wx.navigateTo({
      url: '/packageMy/fansList/fansList?id=' +
        this.data.userId
    })
  },

  privateChat({
    detail
  }) {
    console.log(detail)
  },

  cancelReward() {
    this.setData({
      showReward: false
    })
  },

  signNow(item) {
    const app = getApp()
    request({
      url: '/mini/api/selectTeam/joinGroup',
      method: 'POST',
      data: {
        groupId: Number(
          item.currentTarget.dataset.item.id
        ),
        userId: Number(
          app.globalData.userInfo.userId
        )
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        wx.navigateTo({
          url: '/packageMy/chat/chat?conversationID=' +
            'GROUP' +
            item.currentTarget.dataset.item.id
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  confirmReward({
    detail
  }) {
    this.setData({
      showReward: false
    })
    request({
      url: '/mini/api/findNeighbor/reward',
      method: 'POST',
      data: {
        amount: Number(detail.selectPrice),
        userId: Number(this.data.userId)
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        wx.showToast({
          title: '打赏成功',
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  reward() {
    this.setData({
      showReward: true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getIsShowTeam()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (
      this.data.showLoading ||
      this.data.showNoMore
    ) {
      return
    }
    let newPage = this.data.page + 1
    this.setData({
      showLoading: true,
      page: newPage
    })
    this.getDataList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})